<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>kdist.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>d</span>=<span class=defun_name>kdist</span>(<span class=defun_in>X,model</span>)<br>
<span class=h1>%&nbsp;KDIST&nbsp;Computes&nbsp;distance&nbsp;between&nbsp;vectors&nbsp;in&nbsp;kernel&nbsp;space.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;d&nbsp;=&nbsp;kdist(X,model)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;It&nbsp;computes&nbsp;distance&nbsp;between&nbsp;vectors&nbsp;mapped&nbsp;into&nbsp;the&nbsp;feature&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;space&nbsp;induced&nbsp;by&nbsp;the&nbsp;kernel&nbsp;function&nbsp;(model.options.ker,</span><br>
<span class=help>%&nbsp;&nbsp;model.options.arg).&nbsp;The&nbsp;distance&nbsp;is&nbsp;computed&nbsp;between&nbsp;images</span><br>
<span class=help>%&nbsp;&nbsp;of&nbsp;vectors&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;mapped&nbsp;into&nbsp;feature&nbsp;space</span><br>
<span class=help>%&nbsp;&nbsp;and&nbsp;a&nbsp;point&nbsp;in&nbsp;the&nbsp;feature&nbsp;space&nbsp;given&nbsp;by&nbsp;model:</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;d(i)&nbsp;=&nbsp;kernel(X(:,i),X(:,i))&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;2*kernel(X(:,i),models.sv.X)*model.Alpha&nbsp;+&nbsp;b,</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;where&nbsp;b&nbsp;[1x1]&nbsp;is&nbsp;assumed&nbsp;to&nbsp;be&nbsp;equal&nbsp;to&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;model.b&nbsp;=&nbsp;model.Alpha'*kernel(model.sv.X)*model.Alpha.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Input&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Deternines&nbsp;a&nbsp;point&nbsp;of&nbsp;the&nbsp;feature&nbsp;space:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;1]&nbsp;Multipliers.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;nsv]&nbsp;Vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options.ker&nbsp;[string]&nbsp;Kernel&nbsp;identifier&nbsp;(see&nbsp;'help&nbsp;kernel').</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options.arg&nbsp;[1&nbsp;x&nbsp;nargs]&nbsp;Kernel&nbsp;argument(s).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;d&nbsp;[num_data&nbsp;x&nbsp;1]&nbsp;Distance&nbsp;between&nbsp;vectors&nbsp;in&nbsp;the&nbsp;feature&nbsp;space.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;model.Alpha&nbsp;=&nbsp;dualmean(size(data.X,2));</span><br>
<span class=help>%&nbsp;&nbsp;model.sv.X&nbsp;=&nbsp;data.X;</span><br>
<span class=help>%&nbsp;&nbsp;model.options.ker&nbsp;=&nbsp;'rbf';</span><br>
<span class=help>%&nbsp;&nbsp;model.options.arg&nbsp;=&nbsp;0.25;</span><br>
<span class=help>%&nbsp;&nbsp;model.b&nbsp;=&nbsp;model.Alpha'*kernel(data.X,'rbf',0.25)*model.Alpha;</span><br>
<span class=help>%&nbsp;&nbsp;[Ax,Ay]&nbsp;=&nbsp;meshgrid(linspace(-5,5,100),&nbsp;linspace(-5,5,100));</span><br>
<span class=help>%&nbsp;&nbsp;dist&nbsp;=&nbsp;kdist([Ax(:)';Ay(:)'],model);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;ppatterns(data.X);&nbsp;contour(&nbsp;Ax,&nbsp;Ay,&nbsp;reshape(dist,100,100));</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;MINBALL.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;25-aug-2004,&nbsp;VF,&nbsp;MINBALL&nbsp;added&nbsp;to&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help1>%&nbsp;16-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;26-feb-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;13-sep-2002,&nbsp;VF</span><br>
<span class=help1>%&nbsp;15-jun-2002,&nbsp;VF</span><br>
<br>
<hr>
[dim,num_data]=size(X);<br>
<br>
x2&nbsp;=&nbsp;diagker(&nbsp;X,&nbsp;model.options.ker,&nbsp;model.options.arg);<br>
<br>
Ksvx&nbsp;=&nbsp;kernel(&nbsp;X,&nbsp;model.sv.X,&nbsp;model.options.ker,&nbsp;model.options.arg);<br>
<br>
d&nbsp;=&nbsp;sqrt(&nbsp;x2&nbsp;-&nbsp;2*Ksvx*model.Alpha(:)&nbsp;+&nbsp;model.b*ones(num_data,1)&nbsp;);<br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
